#include "mqttscene.h"

MqttScene::MqttScene(QWidget *parent) : QGraphicsScene(parent)
{
    mBackImg = "D:/123.jpg";
}

MqttScene::~MqttScene()
{

}

bool MqttScene::setBackImg(QString imgFile)
{
    mBackImg = imgFile;
}

void MqttScene::drawBackground(QPainter *painter, const QRectF &rect)
{
    if(views().count()==0)return;

    // 计算视窗的大小,消除图元拖动时出现的残影
    QGraphicsView* pView=views().first();
    QRect contentRect=pView->viewport()->contentsRect();
    QRectF sceneRect =pView->mapToScene(contentRect).boundingRect();

    //绘制指定图片作为背景
    QPixmap pixmap(mBackImg);
    painter->drawPixmap(sceneRect,pixmap,QRect());
}
